package com.zhou.common.mybatis.template;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class CodeGeneratorUpdate {

    /**
     * 如果当前类在子模块式，需要指定
     */
    private static final String moduleName = "mybati-plus-template";

    static String mysqlUrl = "jdbc:mysql://rm-d2e60bvmcs6u14zvd.mysql.rds.ops.cmcczzcloud.com:3306/pre_release_qinqing_online?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useTimezone=true&serverTimezone=Asia/Shanghai";
    static String username = "qqtest_admin";
    static String password = "admin@123=";

//    static String mysqlUrl = "jdbc:mysql://rm-8vbarrz5728we848fvo.mysql.zhangbei.rds.aliyuncs.com:3306/my-management?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useTimezone=true&serverTimezone=Asia/Shanghai";
//    static String username = "zxf_1";
//    static String password = "Zxf@123!";

    public static void main(String[] args) {
        String module = org.apache.commons.lang3.StringUtils.isNoneEmpty(moduleName) ? "/" + moduleName : "";
        String projectPath = System.getProperty("user.dir") + module;
        String author = "zxf";
        String moduleName = "template";

        FastAutoGenerator.create(mysqlUrl, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride()
                            .disableOpenDir()
                            .outputDir(projectPath + "/src/test/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(CodeGeneratorUpdate.class.getPackage().getName()) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/test/resources/mybatis")); // 设置mapperXml生成路径

                })
                .strategyConfig(builder -> {
                    builder.addInclude(getTables(scanner("请输入表名，多个英文逗号分隔？所有输入 all"))).addTablePrefix("enterprise_")// 设置需要生成的表名
                            .entityBuilder()
                            .enableLombok()
//                            .superClass("com.zhou.common.lib.model.BaseEntity")
//                            .addSuperEntityColumns("del_flag", "creator_id", "creator_name", "created_time"
//                                    , "modifier_id", "modifier_name", "modified_time")
                            .entityBuilder()
                            .controllerBuilder().enableRestStyle()
                    ; // 设置过滤表前缀
                })
                .templateConfig(builder -> {
                    //builder.controller("").service("").serviceImpl("").mapper("").mapperXml("").entity("");
//                    builder.controller("").service("").serviceImpl("").mapper("").entity("");
                    builder.controller("");
//                    builder.mapperXml("");

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

}
